@extends('admin.base')

@section('content')
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">

            <div class="layui-form-item">
                <span style="font-size: 18px;font-weight:bold;">{{$renter->name??""}}</span>
                <div class="layui-input-inline" style="float: right;width: auto;" >
                    <button class="layui-btn  caller-fr" id="addmedia">添加站点</button>
                </div>

            </div>
        </div>
        <div class="layui-card-body">
            <table id="dataTable" lay-filter="dataTable"></table>
            <script type="text/html" id="options">
                <div class="layui-btn-group">
                    @can('changzu.site.edit')
                        <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
                        <a class="layui-btn layui-btn-sm" lay-event="linkdevice">关联机器</a>
                    @endcan
                    @can('changzu.site.destroy')
                        <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
                    @endcan
                </div>
            </script>
            <script type="text/html" id="district">
                @{{#  if(d.district){ }}
                @{{d.district.name}}
                @{{#  } }}
            </script>
        </div>
    </div>
@endsection

@section('script')
    @can('changzu.site')
        <script>
            layui.use(['layer', 'table', 'form'], function () {
                var $ = layui.jquery;
                var layer = layui.layer;
                var form = layui.form;
                var table = layui.table;
                var modal;
                //用户表格初始化
                var dataTable = table.render({
                    elem: '#dataTable'
                    , autoSort: false
                    , height: 500
                    , url: "{{ route('admin.site.data',['renter_id' => $renter_id]) }}" //数据接口
                    , page: true //开启分页
                    , cols: [[ //表头
                        {checkbox: true, fixed: true}
                        , {field: 'id', title: 'ID', sort: true, width: 60}
                        , {field: 'name', title: '名称'}
                        , {field: 'region_name', title: '区域'}
                        , {field: 'address', title: '地址'}
                        , {field: 'longitude', title: '经度'}
                        , {field: 'latitude', title: '纬度'}
                        , {field: 'yysj', title: '营业时间',templet:function (d){
                                return d.bh_start+' - '+d.bh_end;
                            }}
                        , {field: 'status', title: '状态',templet:function (d){
                             if (d.device != null ){
                                return '<font color="grenn">已关联</font>';
                             }else{
                                return '未关联设备';
                             }
                        }}
                        , {fixed: 'right', width: 150, align: 'center', toolbar: '#options'}
                    ]]
                    ,done: function(res, curr, count){
                        //添加媒体
                        $("#addmedia").click(function (){
                            modal = layer.open({
                                type: 2,
                                area: ['40%', '90%'],
                                content: '/admin/site/create?renter_id={{$renter_id}}',
                                success: function (res) {
                                    console.log(res);
                                    if (res.code == 0){
                                        layer.close(modal);
                                        // table.reload('dataTable');
                                    }
                                }
                            });
                        });
                    }
                });
                //监听工具条
                table.on('tool(dataTable)', function (obj) { //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
                    var data = obj.data //获得当前行数据
                        , layEvent = obj.event; //获得 lay-event 对应的值
                    if (layEvent === 'del') {
                        var str = '确认删除吗？';
                        layer.confirm(str , function (index) {
                            layer.close(index);
                            var load = layer.load();
                            $.post("{{ route('admin.site.destroy') }}", {
                                _method: 'post',
                                ids: [data.id]
                            }, function (res) {
                                layer.close(load);
                                if (res.code == 0) {
                                    layer.msg(res.msg, {icon: 1}, function () {
                                        obj.del();
                                    })
                                } else {
                                    layer.msg(res.msg, {icon: 2})
                                }
                            });
                        });
                    } else if (layEvent === 'edit') {
                         location.href = '/admin/site/edit?id=' + data.id;
                        //top.layui.index.openTabsPage('/admin/media/edit?id=' + data.id +'&estate_id=', '编辑点位');
                    }else if (layEvent === 'linkdevice'){
                        modal2 = layer.open({
                            type: 2,
                            title: '关联机器',
                            area: ['60%', '100%'],
                            content: '/admin/site/binddevice?id=' + data.id,
                            success: function (res) {
                                //console.log(res);
                                if (res.code == 0){
                                    layer.close(modal);
                                    // table.reload('dataTable');
                                }
                            }
                        });
                    }
                });


                //按钮批量删除
                $("#listDelete").click(function () {
                    var ids = [];
                    var hasCheck = table.checkStatus('dataTable');
                    var hasCheckData = hasCheck.data;
                    if (hasCheckData.length > 0) {
                        $.each(hasCheckData, function (index, element) {
                            ids.push(element.id)
                        })
                    }
                    if (ids.length > 0) {
                        layer.confirm('确认删除吗', function (index) {
                            layer.close(index);
                            var load = layer.load();
                            $.post("{{ route('admin.site.destroy') }}", {
                                _method: 'delete',
                                ids: ids
                            }, function (res) {
                                layer.close(load);
                                if (res.code == 0) {
                                    layer.msg(res.msg, {icon: 1}, function () {
                                        dataTable.reload({page: {curr: 1}});
                                    })
                                } else {
                                    layer.msg(res.msg, {icon: 2})
                                }
                            });
                        });
                    } else {
                        layer.msg('请选择删除项', {icon: 2})
                    }
                })

                //搜索
                $("#searchBtn").click(function () {
                    var name = $("#name").val();
                    dataTable.reload({
                        where: {name: name},
                        page: {curr: 1}
                    })
                });


            })
        </script>
    @endcan
@endsection
